package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /*
    * 将集合转化为字符串格式的数据,数据之间使用","分隔
    * */
    private String listToString(List list) {
        if (list==null || list.size()==0) {
            return "";
        }
        StringJoiner joiner = new StringJoiner(",");
        for (Object o : list) {
            joiner.add(o.toString());
        }
        return joiner.toString();
    }

    /*
    * 统计营业额
    * */
    @Override
    public TurnoverReportVO getTurnoverReport(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList=new ArrayList<>();
        //获取日期遍历集合
        while (begin.isBefore(end)) {
            dateList.add(begin);
            begin=begin.plusDays(1);
        }
        dateList.add(end);
        //将日期集合转换为字符串,日期之间用','分隔
        String dateString = listToString(dateList);

        //查询订单表中时间范围内状态为已完成的订单总金额
        List<BigDecimal> amountList=new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //获取当天营业额
            Map<String,Object> map=new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status",Orders.COMPLETED);

            BigDecimal amount = orderMapper.getAmountByMap(map);
            if (amount==null) {
                amount = BigDecimal.ZERO;
            }
            //记录到营业额集合当中
            amountList.add(amount);
        }
        String amountString = listToString(amountList);
        return new TurnoverReportVO(dateString,amountString);
    }

    /*
     * 统计用户
     * */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList=new ArrayList<>();
        //获取日期遍历集合
        while (begin.isBefore(end)) {
            dateList.add(begin);
            begin=begin.plusDays(1);
        }
        dateList.add(end);
        //将日期集合转换为字符串,日期之间用','分隔
        String dateString = listToString(dateList);
        //遍历日期获取当天用户总数和上一日用户总数
        Integer totalUser =0;
        Integer newUser=0;
        List<Integer> totalUserList =new ArrayList<>();
        List<Integer> newUserList=new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String,Object> map=new HashMap<>();
            map.put("end",endTime);
            totalUser = userMapper.getUserNumber(map);
            totalUserList.add(totalUser);
            map.put("begin",beginTime);
            newUser= userMapper.getUserNumber(map);
            newUserList.add(newUser);
        }
        String totalUserString = listToString(totalUserList);
        String newUserString = listToString(newUserList);

        return new UserReportVO(dateString, totalUserString,newUserString);
    }
}
